<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Hello React</title>
</head>
<body>

  <div id="root"></div>

  <script src="../lib/react.js"></script>
  <script src="../lib/react-dom.js"></script>
  <script src="../lib/babel.js"></script>
  
  <script type="text/babel">
    
    class App extends React.Component {
      
      constructor() {
        super()
        this.state = {
          counter: 0,
          isReady: false,
          friend: {
            name: '张三',
            age: 18
          }
        }
      }
      change() {
        this.setState({
          isReady: !this.state.isReady,
          friend: null
        })
      }

      render() {
        const { counter, isReady, friend } = this.state

        let showElement = null
        if(isReady) {
          showElement = (<div>准备好了</div>)
        } else {
          showElement = (<div>没准备好</div>)
        }

        return (
          <div>
            {/* 方式1： if条件判断 */}
            <div>{showElement}</div>
            {/* 方式2： 三元运算符 */}
            <div>{isReady ? <div>准备好了</div> : <div>没准备好</div>}</div>
            {/* 方式2： 逻辑与运算符 */}
            <div>{ friend && <div>名字: {friend.name}</div> }</div>

            <button onClick={() => this.change()}>切换显示</button>
          </div>
        )
      }
    }

    // 将组件渲染到页面
    const root = ReactDOM.createRoot(document.querySelector("#root"))
    root.render(<App />)
  </script>
</body>
</html>